#pragma once
#include "Matrix4.h"
#include "Vector3.h"
#include "Vector4.h"
#include "Mesh.h"
#include <vector>

class SceneNode {
public:
	SceneNode(Mesh*m = NULL , Vector4 colour = Vector4(1,1,1,1));
	virtual ~SceneNode();

	void SetTransform(const Matrix4 &matrix) { transform = matrix; }
	const Matrix4& GetTransform() const { return transform; }
	Matrix4 GetWorldTransform() const { return worldTransform; }
	Vector4 GetColour() const { return colour; }
	void SetColour(Vector4 c) { colour = c; }
	Vector3 GetModelScale() const { return modelScale; }
	void SetModelScale(Vector3 s) { modelScale = s; }
	Mesh* GetMesh() const { return mesh; }

	void SetShader(Shader** s){ shader = s; };
	Shader** GetShader()	{ return shader; };

	void SetType(int t){ Type = t; };
	int GetType()		{ return Type; };

	void SetMesh(Mesh*m) { mesh = m; }

	//void SetTexture(GLuint tex){mesh->SetTexture(texture);}
	//GLuint GetTexture()const {return texture;}

	void AddChild(SceneNode* s);
	void RemoveChild(SceneNode* s);
	virtual void Update(float msec);
	virtual void Draw(OGLRenderer& r);

	void SetDraw(bool d){ 
		for(vector <SceneNode*>::iterator i = children.begin(); i != children.end(); ++i) {
			(*i)->SetDraw(d);
		}
		draw = d; 
	}

	std::vector <SceneNode*>::const_iterator GetChildIteratorStart() {
		return children.begin();}

	std::vector <SceneNode*>::const_iterator GetChildIteratorEnd() {
		return children.end();}

	float GetBoundingRadius () const {return boundingRadius ;}
	void SetBoundingRadius(float f) {boundingRadius = f;}

	float GetCameraDistance () const {return distanceFromCamera ;}
	void SetCameraDistance(float f) {distanceFromCamera = f;}

	static bool CompareByCameraDistance(SceneNode*a,SceneNode*b) {
		return (a->distanceFromCamera <
			b->distanceFromCamera) ? true : false;
	}

	float GetTextureAspectRatio(){ return aspectRatio; }
	void SetTextureAspectRatio(float a){ aspectRatio = a; }
protected:
	Mesh* mesh;
	bool draw;
	SceneNode* parent;
	
	Matrix4 worldTransform;
	Matrix4 transform;
	Vector3 modelScale;
	Vector4 colour;
	std::vector <SceneNode*> children;
	Shader** shader;

	float distanceFromCamera;
	float boundingRadius;

	//GLuint texture;

	int Type;
	
	float aspectRatio;
};